// 搜索栏功能
document.querySelector('.search-bar input').addEventListener('focus', function() {
    this.parentElement.style.backgroundColor = '#fff';
});

document.querySelector('.search-bar input').addEventListener('blur', function() {
    this.parentElement.style.backgroundColor = '#f0f0f0';
});

// 底部导航切换
document.querySelectorAll('.nav-item').forEach(item => {
    item.addEventListener('click', function() {
        document.querySelectorAll('.nav-item').forEach(nav => {
            nav.classList.remove('active');
        });
        this.classList.add('active');
    });
});

// 快捷功能区和服务项点击效果
const clickableItems = document.querySelectorAll('.quick-item, .service-item');
clickableItems.forEach(item => {
    item.addEventListener('click', function() {
        this.style.transform = 'scale(0.95)';
        setTimeout(() => {
            this.style.transform = 'scale(1)';
        }, 100);
    });
});

// 发热门诊项目点击效果
document.querySelectorAll('.fever-item').forEach(item => {
    item.addEventListener('click', function() {
        this.style.transform = 'scale(0.95)';
        setTimeout(() => {
            this.style.transform = 'scale(1)';
        }, 100);
    });
});

// 文章项目点击效果
document.querySelectorAll('.article-item').forEach(item => {
    item.addEventListener('click', function() {
        this.style.transform = 'translateX(5px)';
        setTimeout(() => {
            this.style.transform = 'translateX(0)';
        }, 100);
    });
});

// 轮播图功能
const bannerWrapper = document.querySelector('.banner-wrapper');
const dots = document.querySelectorAll('.dot');
const prevBtn = document.querySelector('.banner-btn.prev');
const nextBtn = document.querySelector('.banner-btn.next');
let currentSlide = 0;
const totalSlides = document.querySelectorAll('.banner').length;

function updateSlide(index) {
    bannerWrapper.style.transform = `translateX(-${index * 100}%)`;
    dots.forEach((dot, i) => {
        dot.classList.toggle('active', i === index);
    });
    currentSlide = index;
}

// 自动轮播
let autoSlideInterval = setInterval(() => {
    let nextIndex = (currentSlide + 1) % totalSlides;
    updateSlide(nextIndex);
}, 5000);

// 点击圆点切换
dots.forEach((dot, index) => {
    dot.addEventListener('click', () => {
        clearInterval(autoSlideInterval);
        updateSlide(index);
        autoSlideInterval = setInterval(() => {
            let nextIndex = (currentSlide + 1) % totalSlides;
            updateSlide(nextIndex);
        }, 5000);
    });
});

// 上一张/下一张按钮
prevBtn.addEventListener('click', () => {
    clearInterval(autoSlideInterval);
    let prevIndex = (currentSlide - 1 + totalSlides) % totalSlides;
    updateSlide(prevIndex);
    autoSlideInterval = setInterval(() => {
        let nextIndex = (currentSlide + 1) % totalSlides;
        updateSlide(nextIndex);
    }, 5000);
});

nextBtn.addEventListener('click', () => {
    clearInterval(autoSlideInterval);
    let nextIndex = (currentSlide + 1) % totalSlides;
    updateSlide(nextIndex);
    autoSlideInterval = setInterval(() => {
        let nextIndex = (currentSlide + 1) % totalSlides;
        updateSlide(nextIndex);
    }, 5000);
});

// 触摸滑动支持
let touchStartX = 0;
let touchEndX = 0;

bannerWrapper.addEventListener('touchstart', (e) => {
    touchStartX = e.touches[0].clientX;
    clearInterval(autoSlideInterval);
});

bannerWrapper.addEventListener('touchmove', (e) => {
    touchEndX = e.touches[0].clientX;
    const diff = touchStartX - touchEndX;
    const sensitivity = 50; // 滑动灵敏度
    
    if (Math.abs(diff) > sensitivity) {
        if (diff > 0 && currentSlide < totalSlides - 1) {
            updateSlide(currentSlide + 1);
        } else if (diff < 0 && currentSlide > 0) {
            updateSlide(currentSlide - 1);
        }
        touchStartX = touchEndX;
    }
});

bannerWrapper.addEventListener('touchend', () => {
    autoSlideInterval = setInterval(() => {
        let nextIndex = (currentSlide + 1) % totalSlides;
        updateSlide(nextIndex);
    }, 5000);
}); 